package com.kpx.app.utils;

import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * 创建时间： 2015/7/1 15:26
 * 作者：Ryan
 * 类描述：动画特效
 */
public class AnimationUtils {

    public static void afterLoadingAnim(View viewContainer) {
        afterLoadingAnim(viewContainer, 0.9f);
    }

    public static void afterLoadingAnim(View viewContainer, float scaleStart) {
        afterLoadingAnim(viewContainer, scaleStart, 450);
    }

    public static void afterLoadingAnim(View viewContainer, float scaleStart, int duration) {
        viewContainer.setVisibility(View.VISIBLE);
        ViewCompat.setScaleY(viewContainer, scaleStart);
        ViewCompat.setScaleX(viewContainer, scaleStart);
        ViewCompat.animate(viewContainer).scaleX(1).setDuration(duration).start();
        ViewCompat.animate(viewContainer).scaleY(1.0f).setDuration(duration).start();
    }

    public static void expand(final View view, Animation.AnimationListener animationListener) {
        expand(view, 250, animationListener);
    }


    public static void collapse(final View view, Animation.AnimationListener animationListener) {
        collapse(view, 250, animationListener);
    }

    public static void expand(final View view, int duration, Animation.AnimationListener animationListener) {
        view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targetHeight = view.getMeasuredHeight();
        view.getLayoutParams().height = 0;
        view.setVisibility(View.VISIBLE);
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                view.getLayoutParams().height = interpolatedTime == 1 ? ViewGroup.LayoutParams.WRAP_CONTENT : (int) (targetHeight * interpolatedTime);
                view.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animation.setDuration(duration);
        animation.setAnimationListener(animationListener);
        view.startAnimation(animation);
    }

    public static void collapse(final View view, int duration, Animation.AnimationListener animationListener) {
        final int initialHeight = view.getMeasuredHeight();
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1) {
                    view.setVisibility(View.GONE);
                } else {
                    view.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                    view.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animation.setDuration(duration);
        animation.setAnimationListener(animationListener);
        view.startAnimation(animation);
    }

    public static void expandHorizantal(final View view, Animation.AnimationListener animationListener) {
        view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targetWidth = view.getMeasuredWidth();

        view.getLayoutParams().width = 0;
        view.setVisibility(View.VISIBLE);
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                view.getLayoutParams().width = interpolatedTime == 1 ? ViewGroup.LayoutParams.WRAP_CONTENT : (int) (targetWidth * interpolatedTime);
                view.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animation.setDuration(250);
        animation.setAnimationListener(animationListener);
        view.startAnimation(animation);
    }

    public static void collapseHorizantal(final View view, Animation.AnimationListener animationListener) {
        final int initialWidth = view.getMeasuredWidth();
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1) {
                    view.setVisibility(View.GONE);
                } else {
                    view.getLayoutParams().width = initialWidth - (int) (initialWidth * interpolatedTime);
                    view.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animation.setDuration(250);
        animation.setAnimationListener(animationListener);
        view.startAnimation(animation);
    }

}
